<template>
	<view class="fun-menu-page" @click="jump">
		<view class="menu-icon">
			<image :src="menu.iconPath" :style="{ width: `${size}px`, height: `${size}px`}"/>
		</view>
		<view class="menu-name">{{ menu.name }}</view>
	</view>
</template>

<script setup lang="ts">
	import { PropType , ref, computed} from 'vue';
	import { useBound } from "@/hooks/useBound.ts"
	import { isLogged } from "@/utils/mock.js"
	import { timeFormatHelp } from "@/utils/index"
	import { useStudentListStore } from "@/stores/useStudentList.js"
	
	const {tipBoundStudent, studentIsEmpt} = useBound();
	const studentStore = useStudentListStore();
	const curStud = computed(() => studentStore.currentStudent);
	const vipExpireDate = computed(() => {
		if(studentIsEmpt.value){
			return false;
		}
		if(curStud.value?.expireDate){
			const day = timeFormatHelp.daysDifference(curStud.value.expireDate);
			return day <= 0
		} else {
			return false
		}
	})
	
	type menuProps = {
		iconPath: string, pageJumpPathUrl: string, name: string, type?:{mode: 'kefu', callback?:() => void}
	}
	const props =  defineProps({
		menu:{
			type: Object as PropType<menuProps>,    
			required: true,
		},
		size: {
			type: Number,
			default: 40
		}
	})
	
	const jump = () => {
		try{
			const jumpPath = props?.menu?.pageJumpPathUrl;
			const { mode = [], callback = () => {}} = props?.menu?.type || {}
			const haveMock = mode.includes("mock")
			const haveKefu = mode.includes("kefu")
			const isJump = mode.includes("jump")
			if(isJump){ // 直接跳转，不做任何判断
				if(vipExpireDate.value){
					uni.showModal({
						title:"您的孩子VIP已到期",
						content:"温馨提示， 尊敬的家长，您的孩子的账号 VIP 已到期，管控功能即将失效，避免影响使用，请立即续费, 感谢您的支持！",
						confirmText:'购买会员',
						success(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pagesB/memberCenter/index'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return 
				}
				uni.navigateTo({
					url:jumpPath
				})
				return 
			}
			if((studentIsEmpt.value || haveMock) && !haveKefu){
				console.log('不回来这里吧', mode)
				if(haveMock && !isLogged()){
					// const pass = ['authCheck', 'help']
					const bool = (mode.includes('authCheck') || mode.includes('help')) ? false : haveMock
					tipBoundStudent(bool);
					if(bool === false){
						return
					}
				} 
				if(isLogged() && studentIsEmpt.value){
					tipBoundStudent()
					return;
				}
			}
			
			if(!isLogged() && haveKefu){ // fix 【ID1000152】未登录账号的问题
				console.log("是否会到这里来")
				tipBoundStudent();
				return;
			}
			
			if(mode && haveKefu){
				callback?.()
				return;
			}
			
			if(jumpPath) {
				if(vipExpireDate.value){
					uni.showModal({
						title:"您的孩子VIP已到期",
						content:"温馨提示， 尊敬的家长，您的孩子的账号 VIP 已到期，管控功能即将失效，避免影响使用，请立即续费",
						confirmText:'购买会员',
						success(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pagesB/memberCenter/index'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return 
				}
				uni.navigateTo({
					url:jumpPath
				})
			}
		}catch(e){
			//TODO handle the exception
		}
	}
</script>

<style scoped lang="scss">
	.fun-menu-page{
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		width: 180upx;
		height: 130upx;
		.menu-icon{
			display: flex;
			justify-content: center;
			// margin-bottom: 20upx;
			// width: 82upx;
			// height: 82upx;
		}
		.menu-name{
			font-family: PingFang SC, PingFang SC;
			// font-weight: 600;
			font-size: 28upx;
			color: #333333;
		}
	}
</style>